# -*- coding: utf-8 -*-
# @Time    : 2017/12/20 18:20
# @Author  : David.Dong
# @Email   : dww102@gmail.com
# @Software: PyCharm Community Edition

from SVMAlgo import SMOAlgo
import datetime
import matplotlib.pyplot as plt
import numpy as np

dataArr, labelArr = SMOAlgo.loadDataSet('testSet.txt')

#draw begin
# 数据准备
dataArr, labelArr = SMOAlgo.loadDataSet('testSet.txt')
# 数据颜色分类绘制数据
datingDataMatProp1 = np.array([0., 0.])  # [0., 0., 0.]效果相同，但是初始化的时候数据类型不同，np.array([])为{ndarray}，[]为{list}
datingDataMatProp2 = np.array([0., 0.])
index = 0
# matplotlib 图形化
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1) #441-第一个数为行，第二个为列，第三个为次序
#分颜色绘制数据分类
for colorLabel in labelArr:
    if colorLabel == -1: #
        datingDataMatProp1 = np.row_stack((datingDataMatProp1, dataArr[index])) #矩阵中添加一行
    else:
        datingDataMatProp2 = np.row_stack((datingDataMatProp2, dataArr[index])) #矩阵中添加一行
    index += 1

# 数据处理，去除第一行额外的数据
datingDataMatProp1 = np.delete(datingDataMatProp1, 0, 0)
datingDataMatProp2 = np.delete(datingDataMatProp2, 0, 0)

# 绘图 [:, 0]:1列，[:, 1]:2列
type1 = ax.scatter(datingDataMatProp1[:, 0], datingDataMatProp1[:, 1], c='red', s=30)
type2 = ax.scatter(datingDataMatProp2[:, 0], datingDataMatProp2[:, 1], c='blue', s=20)

# 加标签
ax.legend((type1, type2), ('第一列', '第二列'), loc=0)
plt.xlabel('第一列')
plt.ylabel('第二列')
plt.title('分类图')
plt.legend()

# 显示图形
plt.show()

#draw end

print(labelArr)
print(dataArr)

beforetime = datetime.datetime.now()

b, alphas = SMOAlgo.smoSimple(dataArr, labelArr, 0.6, 0.001, 40)

aftertime = datetime.datetime.now()
print(aftertime - beforetime)

print(b)
print(alphas)

